home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / others / ole_101.zip / PATRON.ZIP / OLEMENU.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  8KB  |  265 lines

  1. /*
  2.  * OLEMENU.C
  3.  *
  4.  * Functions to enable or disable OLE menu items as well as
  5.  * menu items depending on the state of the clipboard.
  6.  *
  7.  * Copyright(c) Microsoft Corp. 1992 All Rights Reserved
  8.  *
  9.  */
  10.  
  11. #include <windows.h>
  12. #include <ole.h>
  13. #include "register.h"
  14. #include "oclient.h"
  15.  
  16.  
  17.  
  18. /*
  19.  * MenuOLEClipboardEnable
  20.  *
  21.  * Purpose:
  22.  *  Enabled or disables Edit menu commands for Paste, PasteLink, Paste
  23.  *  Special, and Links, depending on whether or not certain clipboard
  24.  *  formats exists or if there are linked objects.
  25.  *
  26.  *  This function should be called for the WM_INITPOPUPMENU message in the
  27.  *  main window procedure.
  28.  *
  29.  * Parameters:
  30.  *  hMenu           HMENU of the Edit popup menu.
  31.  *  pDoc            LPDOCUMENT owner of all objects.
  32.  *  pWID            LPWORD to an array of four WORDs, where the caller
  33.  *                  stores menu ID values for Paste, PasteLink, PasteSpecial,
  34.  *                  and Links.  If any of these are zero that menu item is
  35.  *                  ignored.
  36.  *
  37.  * Return Value:
  38.  *  None
  39.  */
  40.  
  41. void FAR PASCAL MenuOLEClipboardEnable(HMENU hMenu, LPDOCUMENT pDoc, LPWORD pWID)
  42.     {
  43.     OLESTATUS   os;
  44.     BOOL        fEmbed;
  45.     BOOL        fLink;
  46.     BOOL        fStatic;
  47.     WORD        wTemp;
  48.  
  49.     if (NULL==pWID)
  50.         return;
  51.  
  52.     /*
  53.      * Check if we can create an embedded, linked, or static object
  54.      * from the clipboard.  Note that olerender_draw means that fourth
  55.      * parameter to OleQueryCreateFromClip is ignored.
  56.      */
  57.  
  58.     os=OleQueryCreateFromClip(PSZOLE(IDS_STDFILEEDITING), olerender_draw, 0);
  59.     fEmbed=(OLE_OK==os);
  60.  
  61.     //Check if we can create a linked object from the clipboard.
  62.     os=OleQueryLinkFromClip(PSZOLE(IDS_STDFILEEDITING), olerender_draw, 0);
  63.     fLink=(OLE_OK==os);
  64.  
  65.     os=OleQueryCreateFromClip(PSZOLE(IDS_STATIC), olerender_draw, 0);
  66.     fStatic=(OLE_OK==os);
  67.  
  68.  
  69.     /*
  70.      * Enable menu items depending on the available formats:
  71.      *  Paste:          If fEmbed or fStatic is TRUE
  72.      *  Paste Link:     Only if fLink if TRUE.
  73.      *  Paste Special:  If fEmbed, fLink, or fStatic is TRUE.
  74.      */
  75.  
  76.     if (0!=pWID[0])
  77.         {
  78.         wTemp=(fEmbed | fStatic) ? MF_ENABLED : (MF_DISABLED | MF_GRAYED);
  79.         EnableMenuItem(hMenu, pWID[0], wTemp | MF_BYCOMMAND);
  80.         }
  81.  
  82.     if (0!=pWID[1])
  83.         {
  84.         wTemp=(fLink) ? MF_ENABLED : (MF_DISABLED | MF_GRAYED);
  85.         EnableMenuItem(hMenu, pWID[1], wTemp | MF_BYCOMMAND);
  86.         }
  87.  
  88.     if (0!=pWID[2])
  89.         {
  90.         wTemp=(fEmbed | fLink | fStatic) ? MF_ENABLED : (MF_DISABLED | MF_GRAYED);
  91.         EnableMenuItem(hMenu, pWID[2], wTemp | MF_BYCOMMAND);
  92.         }
  93.  
  94.  
  95.     /*
  96.      * Enumerate the objects--if we encounter any that IS linked,
  97.      * the FEnumOLEPaste function returns FALSE, so FObjectsEnumerate
  98.      * returns FALSE, which tells us that we HAVE a linked object.
  99.      * Only if FObjectsEnumerate returns TRUE do we disable the item.
  100.      */
  101.  
  102.     if (0!=pWID[3])
  103.         {
  104.         fLink=!FObjectsEnumerate(pDoc, FEnumOLEPaste, 0L);
  105.         wTemp=(fLink) ? MF_ENABLED : (MF_DISABLED | MF_GRAYED);
  106.         EnableMenuItem(hMenu, pWID[3], wTemp | MF_BYCOMMAND);
  107.         }
  108.  
  109.     return;
  110.     }
  111.  
  112.  
  113.  
  114.  
  115.  
  116. /*
  117.  * FEnumOLEPaste
  118.  *
  119.  * Purpose:
  120.  *  Checks each enumerated object for being of type OT_LINK. If
  121.  *  any object is, we return FALSE to say we found one.  We otherwise
  122.  *  return TRUE.  Utterly simple function.
  123.  *
  124.  * Parameters:
  125.  *  pDoc            LPDOCUMENT to the owner of the objects.
  126.  *  pObj            LPOBJECT of the object to delete.
  127.  *  dw              DWORD for extra data, unused.
  128.  *
  129.  * Return Value:
  130.  *  BOOL            TRUE, indicates to continue enumeration.
  131.  */
  132.  
  133. BOOL FAR PASCAL FEnumOLEPaste(LPDOCUMENT pDoc, LPOBJECT pObj, DWORD dw)
  134.     {
  135.     return (OT_LINK!=pObj->dwType);
  136.     }
  137.  
  138.  
  139.  
  140.  
  141. /*
  142.  * MenuOLEVerbAppend
  143.  *
  144.  * Purpose:
  145.  *  Appends the appropriate menu item(s) on the given menu depending
  146.  *  on the given object.  This function finds the verbs for the given
  147.  *  object and if there's one, it creates a single menu item with
  148.  *  <verb><object>.  If there's multiple verbs, it creates a cascading
  149.  *  menu with <object> > <verb 0>
  150.  *                       <verb 1>
  151.  *                         ...
  152.  *
  153.  *  Call this function when processing the WM_INITPOPUPMENU message.
  154.  *
  155.  *  This function requires HVerbEnum function in register.c.
  156.  *
  157.  * Parameters:
  158.  *  hMenu           HMENU of the Edit popup menu.
  159.  *  iVerbMenu       WORD position of the item to modify.
  160.  *  wIDMin          WORD first menu ID value for the menu items.
  161.  *  pDoc            LPDOCUMENT owner of the object which contains clipboard
  162.  *                  formats.
  163.  *  pObj            LPOLEOBJECT pointing to the object used to enumerate
  164.  *                  the verbs to append to the menu.
  165.  *
  166.  * Return Value:
  167.  *  None
  168.  */
  169.  
  170. void FAR PASCAL MenuOLEVerbAppend(HMENU hMenu, WORD iVerbMenu, WORD wIDMin,
  171.                                   LPDOCUMENT pDoc, LPOBJECT pObj)
  172.     {
  173.     LPSTR               pszClass;
  174.     LPSTR               pszVerbs;
  175.     WORD                cf;
  176.     WORD                cVerbs;
  177.     HMENU               hMenuT;
  178.  
  179.     if (NULL==hMenu)
  180.         return;
  181.  
  182.     //Delete whatever menu is in the position we're going to modify.
  183.     DeleteMenu(hMenu, iVerbMenu, MF_BYPOSITION);
  184.  
  185.     if (NULL==pDoc)
  186.         return;
  187.  
  188.     /*
  189.      * If there's no object, then make this menu read "Object" and
  190.      * disable and gray it.
  191.      */
  192.     if (NULL==pObj)
  193.         {
  194.         InsertMenu(hMenu, iVerbMenu, MF_DISABLED | MF_GRAYED | MF_STRING | MF_BYPOSITION,
  195.                    wIDMin, PSZOLE(IDS_OBJECT));
  196.  
  197.         return;
  198.         }
  199.  
  200.     //Retrieve the class name for the object.
  201.     cf=(OT_LINK==pObj->dwType) ? pDoc->cfObjectLink : pDoc->cfOwnerLink;
  202.  
  203.     if (!FObjectDataGet(pObj, cf, pDoc->pszData1))
  204.         {
  205.         //Add the default menu item.
  206.         InsertMenu(hMenu, iVerbMenu, MF_DISABLED | MF_GRAYED | MF_STRING | MF_BYPOSITION,
  207.                    wIDMin, PSZOLE(IDS_OBJECT));
  208.         return;
  209.         }
  210.  
  211.  
  212.     /*
  213.      * Go get the list of verbs for this object; exit if we got no list or
  214.      * no verbs.  We can pass pszClass directly even if it's NULL since
  215.      * hVerbEnum will fail on NULL==pszClass.
  216.      *
  217.      * If there are no verbs or CVerbEnum fails, then default to "Edit."
  218.      * Some old Windows apps register no verbs, so you need to prepare
  219.      * a default.
  220.      */
  221.  
  222.     pszClass=pDoc->pszData1;
  223.     pszVerbs=pDoc->pszData2;
  224.     cVerbs=CVerbEnum(pszClass, pszVerbs, CBSCRATCH);
  225.  
  226.     if (0==cVerbs)
  227.         {
  228.         cVerbs=1;
  229.         lstrcpy(pszVerbs, PSZOLE(IDS_VERBEDIT));
  230.         }
  231.  
  232.     //Change the classname to a descriptive name.
  233.     lstrcpy(pDoc->pszData3, pszClass);
  234.     WDescriptionFromClass(pDoc->pszData3, pszClass, CBSCRATCH);
  235.  
  236.     /*
  237.      * Create a new popup menu and fill it with verbs, if we have more
  238.      * than one verb.  Otherwise add a single menu items in the form
  239.      * of <verb-0><object>.
  240.      */
  241.     if (1==cVerbs)
  242.         {
  243.         wsprintf(pDoc->pszData3, "%s %s &Object", pszVerbs, pszClass);
  244.         InsertMenu(hMenu, iVerbMenu, MF_STRING | MF_BYPOSITION, wIDMin, pDoc->pszData3);
  245.         }
  246.     else
  247.         {
  248.         //Add a popup menu the main menu.
  249.         hMenuT=CreatePopupMenu();
  250.  
  251.         wsprintf(pDoc->pszData3, "%s &Object", pszClass);
  252.         InsertMenu(hMenu, iVerbMenu, MF_STRING | MF_POPUP | MF_BYPOSITION,
  253.                    hMenuT, pDoc->pszData3);
  254.  
  255.         //Add all the verbs to the popup menu.
  256.         while (*pszVerbs)
  257.             {
  258.             InsertMenu(hMenuT, -1, MF_STRING | MF_BYPOSITION, wIDMin++, pszVerbs);
  259.             pszVerbs+=lstrlen(pszVerbs)+1;
  260.             }
  261.         }
  262.  
  263.     return;
  264.     }
  265.